package org.example.tree;



public class HeroNode {

    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public HeroNode() {
    }

    public int getNo() {
        return no;
    }


    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 删除节点，如果删除的节点为叶子节点，那么直接删除
     * 如果不为叶子节点，删除该子树
     */

    public boolean delNode(int no){
        boolean flag = false;
        if (this.left != null && this.left.no == no){
            this.left = null;
            return true;
        }
        if (this.right != null && this.right.no == no){
            this.right = null;
            return true;
        }
        if (this.left != null){
            flag = this.left.delNode(no);
        }
        if (this.right != null){
            flag = this.right.delNode(no);
        }
        return flag;
    }

    //编写前序遍历方法,输出顺序：根左右
    public void preOrder(){
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null){
            this.left.preOrder();
        }
        //递归向右子树前序遍历
        if (this.right != null){
            this.right.preOrder();
        }
    }

    //编写中序遍历方法，输出顺序：左根右
    public void midOrder(){
        if (this.left != null){
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.midOrder();
        }
    }

    //后序扁你方法，输出顺序：左右根
    public void postOrder(){
        if (this.left != null){
            this.left.postOrder();
        }
        if (this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序查找
    public HeroNode preSearch(int no){

        System.out.println("前序查找");
        if (this.no == no ){
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.preSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.right != null){
            resNode = this.right.preSearch(no);
        }
        return resNode;

    }

    //中序查找
    public HeroNode midSearch(int no ){
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.midSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        System.out.println("中序查找");
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            resNode = this.right.midSearch(no);
        }
        return resNode;
    }

    //后序查找
    public HeroNode postSearch(int no){
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.postSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.right != null){
            resNode = this.right.postSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        System.out.println("后序查找");
        if (this.no == no){
            return this;
        }
        return null;
    }

}
